package com.javalong.affairs.module.main.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.utils.AppDataUtils;

/**
 * Created by javalong on 2016/5/15.
 */
public class JLCheckBox extends ImageView implements View.OnClickListener {

    private boolean checked = false;

    public boolean isChecked() {
        return checked;
    }

    public JLCheckBox(Context context) {
        super(context);
        init();
    }

    public JLCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        checked = false;
        setImageResource(R.drawable.checkbox_off);
        setOnClickListener(this);
    }

    public void initWithData(boolean isChecked) {
        checked = isChecked;
        if (isChecked) {
            setImageResource(R.drawable.checkbox_on);
        } else {
            setImageResource(R.drawable.checkbox_off);
        }
    }

    @Override
    public void onClick(View v) {
        checked = !checked;
        if (checked) {
            setImageResource(R.drawable.checkbox_on);
        } else {
            setImageResource(R.drawable.checkbox_off);
        }
        if (onCheckListener != null) {
            onCheckListener.onChecked(this, checked);
        }
    }

    public interface OnCheckListener {
        void onChecked(View v, boolean checked);
    }

    private OnCheckListener onCheckListener;

    public void setOnCheckListener(OnCheckListener onCheckListener) {
        this.onCheckListener = onCheckListener;
    }
}
